﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace ERPStore.ProductRating.Models
{
	[DataContract]
	public class ProductRate
	{
		[DataMember]
		public int Level1Count { get; set; }
		[DataMember]
		public int Level2Count { get; set; }
		[DataMember]
		public int Level3Count { get; set; }
		[DataMember]
		public int Level4Count { get; set; }
		[DataMember]
		public int Level5Count { get; set; }

		[DataMember]
		public int TotalCount
		{
			get
			{
				return Level1Count + Level2Count + Level3Count + Level4Count + Level5Count;
			}
			set
			{
				// For serialization only
			}
		}

		[DataMember]
		public int Average
		{
			get
			{
				var total = TotalCount;
				if (total == 0)
				{
					return 0;
				}
				var rate = Level1Count * 1
							+ Level2Count * 2
							+ Level3Count * 3
							+ Level4Count * 4
							+ Level5Count * 5;

				return Convert.ToInt32(Math.Ceiling(rate / (total * 1.0)));
			}
			set
			{
				// For serialization only
			}
		}

		public int BestRating
		{
			get
			{
				if (Level5Count > 0)
				{
					return 5;
				}
				else if (Level4Count > 0)
				{
					return 4;
				}
				else if (Level3Count > 0)
				{
					return 3;
				}
				else if (Level2Count > 0)
				{
					return 2;
				}
				else if (Level1Count > 0)
				{
					return 1;
				}
				return 0;
			}
			set
			{
				// For serialization only
			}
		}
	}
}
